/*
 * @Author: zc
 * @Date: 2019-11-18 11:43:58
 * @LastEditors: zc
 * @LastEditTime: 2020-09-09 21:28:23
 */

import React, { Suspense } from 'react';
import routes from './routes';
import { BrowserRouter as Router } from 'react-router-dom';
import { RouteWithSubRoutes } from './RouteWithSubRoutes';
import { Loading } from '@/components/Loading';
const { name } = require('@/../package.json');
interface RouterObj {
  path: string;
  component: any;
}

const {
  NODE_ENV,
  REACT_APP_CDN,
  REACT_APP_APITYPE,
  REACT_APP_BUILD,
  REACT_APP_TONGTIANTA
} = process.env;

const isXinPin =
  NODE_ENV === 'production' &&
  REACT_APP_CDN !== 'true' &&
  REACT_APP_APITYPE !== 'dynamic';

let tongtiantaArr: any[] = [];
if (REACT_APP_TONGTIANTA) {
  tongtiantaArr = REACT_APP_TONGTIANTA.split('.jd.com');
}

const _url = `/${name}/${REACT_APP_BUILD}`;
let _routeBase =
  window.location.origin === `${tongtiantaArr[0]}.jd.com`
    ? tongtiantaArr[1]
    : isXinPin
    ? _url
    : '';

const RouterConfig = () => (
  <Router basename={_routeBase}>
    <Suspense fallback={<Loading />}>
      {// @ts-ignore
      routes.map((route: IRouterObj, idx: number) => {
        return <RouteWithSubRoutes key={idx} {...route} />;
      })}
    </Suspense>
  </Router>
);
export default RouterConfig;
